package com.umakr.ax.app.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.umakr.ax.app.service.MsgWebService;
import com.umakr.ax.common.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.ui.ModelMap;
import com.umakr.ax.common.model.Msg;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.umakr.ax.core.base.AbstractController;

/**
 * <p>
 * 消息  前端控制器
 * </p>
 *
 * @author Umakr-Code-Generator
 * @since 2017-12-30
 */
@Controller
@RequestMapping("/api/web/msg")
public class MsgWebController extends AbstractController {
    @Autowired
    MsgWebService msgWebService;

    /**
     * @author 唐腾飞
     * @param modelMap
     * @param rsId
     * @param status
     * @param latestTime
     * @return
     */
    @ApiOperation(value = "获取消息列表 如果是获取历史消息列表 status 值为 '' latestTime 值为 '' ")
    @GetMapping("/listMsg")
    @ResponseBody
    public Object listMsg(ModelMap modelMap,@RequestParam(name = "rsId") Long rsId, @RequestParam(name = "status") Integer status,@RequestParam(name = "latestTime")  @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date latestTime) {
        return setSuccessModelMap(modelMap,msgWebService.listMsg(rsId,status,latestTime));
    }

    /**
     * @author 唐腾飞
     * @param modelMap
     * @param rsId
     * @param status
     * @param latestTime
     * @return
     */
    @ApiOperation(value = "获取消息列表 如果是获取历史消息列表 status 值为 '' latestTime 值为 '' ")
    @GetMapping("/listMoreMsg")
    @ResponseBody
    public Object listMoreMsg(ModelMap modelMap,@RequestParam(name = "rsId") Long rsId, @RequestParam(name = "status") Integer status,@RequestParam(name = "latestTime")  @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date latestTime) {
        return setSuccessModelMap(modelMap,msgWebService.listMoreMsg(rsId,status,latestTime));
    }

    /**
     * @author 唐腾飞
     * @param modelMap
     * @param rsId
     * @param beginMsgTime
     * @param endMsgTime
     * @param msgAttachmentAddr
     * @return
     */
    @ApiOperation(value = "获取历史消息 ")
    @GetMapping("/listMsgHis")
    @ResponseBody
    public Object listMsgHis(ModelMap modelMap,@RequestParam(name = "rsId") Long rsId,@RequestParam(name = "beginMsgTime")  @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date beginMsgTime,@RequestParam(name = "endMsgTime")  @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date endMsgTime,@RequestParam(name = "msgAttachmentAddr") String msgAttachmentAddr) {
        return setSuccessModelMap(modelMap,msgWebService.listMsgHis(rsId,beginMsgTime,endMsgTime,msgAttachmentAddr));
    }


    @ApiOperation(value = "通过/播放/忽略消息")
    @GetMapping("/updateMsgStatus")
    @ResponseBody
    public Object updateMsgStatus(ModelMap modelMap,@RequestParam(name = "ids") String ids, @RequestParam(name = "status") Integer status,@RequestParam(name = "curUserId") Long curUserId,@RequestParam(name = "curUserName") String curUserName) {
        return setSuccessModelMap(modelMap,msgWebService.updateMsgStatus(ids,status,curUserId,curUserName));
    }

    @ApiOperation(value = "忽略消息指定时间之前的消息")
    @GetMapping("/updateMsgStatusByTime")
    @ResponseBody
    public Object updateMsgStatusByTime(ModelMap modelMap,@RequestParam(name = "rsId") Long rsId,@RequestParam(name = "customizeTime")  @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date customizeTime, @RequestParam(name = "status") Integer status,@RequestParam(name = "curUserId") Long curUserId,@RequestParam(name = "curUserName") String curUserName) {
        return setSuccessModelMap(modelMap,msgWebService.updateMsgStatusByTime(rsId,customizeTime,status,curUserId,curUserName));
    }

    @ApiOperation(value = "新消息提示")
    @GetMapping("/getNewMsgCount")
    @ResponseBody
    public Object getNewMsgCount(ModelMap modelMap,@RequestParam(name = "rsId") Long rsId, @RequestParam(name = "latestTime")  @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date latestTime,@RequestParam(name = "status") Integer status) {
        return setSuccessModelMap(modelMap,msgWebService.getNewMsgCount(rsId,latestTime,status));
    }

    @ApiOperation(value = "消息回复")
    @PostMapping("/replyMsg")
    @ResponseBody
    public Object replyMsg(ModelMap modelMap,@RequestParam(name = "msgAttachmentAddr") String msgAttachmentAddr,
                           @RequestParam(name = "msgDirect") Integer msgDirect,
                           @RequestParam(name = "userId") Long userId,
                           @RequestParam(name = "rsId") Long rsId,
                           @RequestParam(name = "messageFrom") Integer messageFrom,
                           @RequestParam(name = "helpId") Long helpId) {
        return setSuccessModelMap(modelMap,msgWebService.replyMsg(msgAttachmentAddr,msgDirect,userId,rsId,messageFrom,helpId));
    }

    @ApiOperation(value = "删除历史消息")
    @GetMapping("/delMsg")
    @ResponseBody
    public Object delMsg(ModelMap modelMap,@RequestParam(name = "ids") String ids) {
        return setSuccessModelMap(modelMap,msgWebService.delMsgs(ids));
    }

    @ApiOperation(value = "首页信息查询")
    @GetMapping("/analysisIndex")
    @ResponseBody
    public Object analysisIndex(ModelMap modelMap,@RequestParam(name = "rsId") Long rsId) {
        return setSuccessModelMap(modelMap,msgWebService.analysisIndex(rsId));
    }

}